package br.com.canvasForAll.ws.resource;

import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import br.com.canvasForAll.model.entity.Canvas;
import br.com.canvasForAll.model.entity.Perfil;
import br.com.canvasForAll.model.services.interfaces.CanvasService;
import br.com.canvasForAll.model.services.interfaces.PerfilService;
import br.com.canvasForAll.model.utils.json.JSONObject;

@Path("canvas")
public class CanvasWS {

	@EJB private CanvasService canvasService;
	@EJB private PerfilService perfilService;
	
	/**
	 * Método para recuperar todos os Canvas ao qual um determinado perfil é o responsável.
	 * @param String id_perfil_ics
	 * @return [JSON] List<Canvas>
	 * */
	@GET
	@Path("/canvasResponsavel/{idResponsavel}")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Canvas> getCanvasPerfil(@PathParam("idResponsavel") String idResponsavel) throws Exception {
		try {
			return (List<Canvas>) canvasService.obterCanvasResponsavel(new Long(idResponsavel));

		} catch(NumberFormatException e) {
			e.printStackTrace();
			throw new WebApplicationException(Status.BAD_REQUEST);

		}catch (Exception e) {
			e.printStackTrace();
			throw new WebApplicationException(Status.NOT_FOUND);
		}
	}
	
	/**
	 * Método para recuperar todos os Canvas ao qual um determinado perfil está como participante.
	 * @param String id_perfil_ics
	 * @return [JSON] List<Canvas>
	 * */
	@GET
	@Path("/canvasParticipante/{idParticipante}")
	@Produces(MediaType.APPLICATION_JSON)
	public List<Canvas> getCanvasParticipante(
			@PathParam("idParticipante") String idParticipante)
			throws Exception {
		try {
			return (List<Canvas>) canvasService.obterCanvasParticipante(new Long(idParticipante));

		}catch (NumberFormatException e){
			e.printStackTrace();
			throw new WebApplicationException(Status.BAD_REQUEST);

		} catch (Exception e) {
			e.printStackTrace();
			throw new WebApplicationException(Status.NOT_FOUND);
		}
	}

	/**
	 * @param [JSON] {"titulo" : "valor", "dataCriacao" : "valor", "dataUltimaAtualizacao" : "valor"}
	 * @param String id_perfil_ics
	 * @return Response {"idCanvasIcs":"valor", "dataUltimaNotificacao":"valor"}
	 * */
	@POST
	@Path("/addCanvas/{id_perfil}")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response addCanvas(@PathParam("id_perfil") String idPerfil, Canvas c) throws Exception {
		try {
			Response response = null;
			
			if (idPerfil != null && c != null) {
				if (verificaCamposObrigatorios(c)) {
					Perfil p = perfilService.obterId(Long.valueOf(idPerfil));
					if (p != null) c.setPerfilResponsavel(p);
					c.setDataUltimaNotificacao(new Date());

					Canvas canvas = canvasService.salvarCanvas(c);
					JSONObject json = new JSONObject();
					json.put("idCanvasIcs", canvas.getId());
					json.put("dataUltimaNotificacao", canvas.getDataUltimaNotificacao().getTime());
					
					response = Response.status(Status.OK).entity(json.toString()).build();

				} else 
					response = Response.status(Status.BAD_REQUEST).build();

			} else 
				response = Response.status(Status.BAD_REQUEST).build();
			
			return response;

		} catch (Exception e) {
			e.printStackTrace();
			throw new WebApplicationException(Status.NOT_FOUND);
		}
	}

	private boolean verificaCamposObrigatorios(Canvas c) {
		if(c.getTitulo() != null && c.getDataCriacao() != null && c.getDataUltimaAtualizacao() != null){
			return true;
		} else {
			return false;
		}
	}

}
